package com.act.moudles.baseAct.service.impl;

import com.act.components.UserComponent;
import com.act.constant.LoginTypeEnums;
import com.act.constant.UserTypeEnums;
import com.act.moudles.baseAct.dao.TaskAnoDao;
import com.act.moudles.baseAct.entity.TaskAno;
import com.act.moudles.user.entity.User;
import com.act.utils.EmptyPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.act.moudles.baseAct.dao.TaskAnoPrisDao;
import com.act.moudles.baseAct.entity.TaskAnoPris;
import com.act.moudles.baseAct.service.TaskAnoPrisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 节点方法/权限配置(TaskAnoPris)表服务实现类
 *
 * @author makejava
 * @since 2023-05-15 10:54:46
 */
@Service("taskAnoPrisService")
public class TaskAnoPrisServiceImpl extends ServiceImpl<TaskAnoPrisDao, TaskAnoPris> implements TaskAnoPrisService {

    @Resource
    private TaskAnoDao taskAnoDao;

    @Resource
    private UserComponent userComponent;

    @Override
    public IPage<TaskAnoPris> pagePris(Page<TaskAnoPris> page, TaskAnoPris taskAnoPris) {
        //1.数据权限
        if(userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()).getUserType() - UserTypeEnums.SYSTEM.getUserType() != 0L){
            List<Long>  anoIdList = taskAnoDao.selectIdByTenantId(userComponent.getUserTenAntId());
            if(CollectionUtils.isEmpty(anoIdList) || !anoIdList.contains(taskAnoPris.getAnoId())){
                return  new EmptyPage();
            }

        }
        return page(page,taskAnoPris.query());
    }

    @Override
    public String addPrivis(TaskAnoPris taskAnoPris) {
        String checkRes = checkUnion(taskAnoPris);
        if(StringUtils.isNotEmpty(checkRes)){
            return checkRes;
        }

        taskAnoPris.setDelFlag(1);
        taskAnoPris.setCreateBy(userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()).getRealName());
        taskAnoPris.setCreateTime(new Date());
        save(taskAnoPris);
        return null;
    }

    @Override
    public String updatePrivis(TaskAnoPris taskAnoPris) {
        String checkRes = checkUnion(taskAnoPris);
        if(StringUtils.isNotEmpty(checkRes)){
            return checkRes;
        }
        taskAnoPris.setUpdateBy(userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()).getRealName());
        taskAnoPris.setUpdateTime(new Date());
        updateById(taskAnoPris);
        return null;
    }

    @Override
    public boolean removePris(Long id) {
        return removeById(id);
    }

    /**
     * 检查重复
     * @param taskAnoPris
     * @return
     */
    private String checkUnion(TaskAnoPris taskAnoPris){
        LambdaQueryWrapper<TaskAnoPris> query = new LambdaQueryWrapper<>();
        query.ne(taskAnoPris.getPrisId() != null,TaskAnoPris::getPrisId,taskAnoPris.getPrisId());
        query.eq(TaskAnoPris::getDelFlag,1);
        query.eq(TaskAnoPris::getPrisCode,taskAnoPris.getPrisCode());
        query.eq(TaskAnoPris::getPrisLoc,taskAnoPris.getPrisLoc());
        query.eq(TaskAnoPris::getAnoId,taskAnoPris.getAnoId());
        long count  = count(query);
        if(count > 1){
            return "当前方法编码已经存在";
        }
        return null;
    }

}

